<template>
  <div class="register-view">
    <div class="register-container">
      <div class="register-card">
        <!-- 注册头部 -->
        <div class="register-header">
          <div class="logo">🏫</div>
          <h1>加入江农二手市场</h1>
          <p>开启你的校园二手交易之旅</p>
        </div>

        <!-- 注册表单 -->
        <el-form
          ref="registerFormRef"
          :model="registerForm"
          :rules="registerRules"
          class="register-form"
          size="large"
          @submit.prevent="handleRegister"
        >
          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="请输入真实姓名"
              :prefix-icon="User"
              clearable
            />
          </el-form-item>

          

          <el-form-item prop="studentId">
            <el-input
              v-model="registerForm.studentId"
              placeholder="请输入学号"
              :prefix-icon="CreditCard"
              clearable
            />
          </el-form-item>

          <el-form-item prop="college">
            <el-select
              v-model="registerForm.college"
              placeholder="请选择学院"
              class="college-select"
            >
              <el-option
                v-for="college in colleges"
                :key="college"
                :label="college"
                :value="college"
              />
            </el-select>
          </el-form-item>

          <el-form-item prop="phone">
            <el-input
              v-model="registerForm.phone"
              placeholder="请输入手机号"
              :prefix-icon="Phone"
              clearable
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              type="password"
              placeholder="请输入密码（至少6位）"
              :prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              placeholder="请确认密码"
              :prefix-icon="Lock"
              show-password
              clearable
            />
          </el-form-item>

          <el-form-item prop="agreement">
            <el-checkbox v-model="registerForm.agreement">
              我已阅读并同意
              <el-link type="primary" href="#" @click.prevent>《用户协议》</el-link>
              和
              <el-link type="primary" href="#" @click.prevent>《隐私政策》</el-link>
            </el-checkbox>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              class="register-btn"
              :loading="userStore.isLoading"
              @click="handleRegister"
              native-type="submit"
            >
              {{ userStore.isLoading ? '注册中...' : '立即注册' }}
            </el-button>
          </el-form-item>
        </el-form>

        <!-- 登录链接 -->
        <div class="login-link">
          <span>已有账号？</span>
          <el-link type="primary" @click="$router.push('/login')">
            立即登录
          </el-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useUserStore } from '@/stores/userStore'
import { useRouter } from 'vue-router'
import { validateJXAUEmail, validatePhone, validateStudentId } from '@/utils/format'
import { 
  User, 
  Message, 
  CreditCard, 
  Phone, 
  Lock 
} from '@element-plus/icons-vue'
import bgUrl from '@/assets/images/background.png'

const bgUrlCss = `url(${bgUrl})`

const userStore = useUserStore()
const router = useRouter()

// 表单引用
const registerFormRef = ref()

// 注册表单数据
const registerForm = reactive({
  username: '',
  studentId: '',
  college: '',
  phone: '',
  password: '',
  confirmPassword: '',
  agreement: false
})

// 学院列表
const colleges = ref([
  '农学院',
  '动物科学技术学院',
  '林学院',
  '生物科学与工程学院',
  '食品科学与工程学院',
  '工学院',
  '信息工程学院',
  '经济管理学院',
  '人文与公共管理学院',
  '理学院',
  '外国语学院',
  '艺术学院',
  '马克思主义学院',
  '体育学院',
  '软件学院',
  '国际教育学院'
])

// 表单验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '姓名长度在2到10个字符', trigger: 'blur' }
  ],
  studentId: [
    { required: true, message: '请输入学号', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (!validateStudentId(value)) {
          callback(new Error('请输入正确的12位学号'))
        } else {
          callback()
        }
      }, 
      trigger: 'blur' 
    }
  ],
  college: [
    { required: true, message: '请选择学院', trigger: 'change' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { 
      validator: (rule, value, callback) => {
        if (!validatePhone(value)) {
          callback(new Error('请输入正确的手机号'))
        } else {
          callback()
        }
      }, 
      trigger: 'blur' 
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  agreement: [
    {
      validator: (rule, value, callback) => {
        if (!value) {
          callback(new Error('请阅读并同意用户协议'))
        } else {
          callback()
        }
      },
      trigger: 'change'
    }
  ]
}

// 处理注册
const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      const success = await userStore.register(registerForm)
      if (success) {
        // 注册成功，跳转到登录页
        router.push('/login')
      }
    }
  })
}
</script>

<style scoped>
.register-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: v-bind('bgUrlCss');
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  background-attachment: fixed;
  padding: 40px 20px;
}

.register-container {
  display: flex;
  justify-content: center;
  max-width: 560px;
  width: 100%;
}

.register-card {
  background: white;
  padding: 44px;
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
  width: 100%;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  font-size: 48px;
  margin-bottom: 15px;
}

.register-header h1 {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.register-header p {
  color: #666;
  font-size: 16px;
}

.register-form {
  margin-bottom: 20px;
}

.college-select {
  width: 100%;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.register-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, #34d399 0%, #10b981 100%);
  border: none;
  border-radius: 8px;
}

.login-link {
  text-align: center;
  color: #666;
}

.register-info {
  color: white;
  padding: 20px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  backdrop-filter: blur(10px);
}

.register-info h3 {
  font-size: 20px;
}
</style>